<% if object.kind? -%>
# rubocop:disable Metrics/CyclomaticComplexity
def self.return_if_object(response, kind)
<% else # object.kind? -%>
def self.return_if_object(response)
<% end # object.kind? -%>
  raise "Bad response: #{response.body}" \
    if response.is_a?(Net::HTTPBadRequest)
  raise "Bad response: #{response}" \
    unless response.is_a?(Net::HTTPResponse)
  return if response.is_a?(Net::HTTPNotFound)
  return if response.is_a?(Net::HTTPNoContent)
<% if object.transport.nil? -%>
  result = JSON.parse(response.body)
<% else # object.transport.nil? -%>
<% if !object.decoder? -%>
  result = JSON.parse(response.body)
<% else # !object.decoder? -%>
<%   if object.kind? -%>
  result = <%= object.transport.decoder -%>(response, kind)
<%   else # object.kind? -%>
  result = <%= object.transport.decoder -%>(response)
<%   end # object.kind? -%>
<% end # !object.decoder? -%>
<% end # object.transport.nil? -%>
  raise_if_errors result, %w[error errors], 'message'
  raise "Bad response: #{response}" unless response.is_a?(Net::HTTPOK)
<% if object.kind? -%>
  raise "Incorrect result: #{result['kind']} (expected '#{kind}')" \
    unless result['kind'] == kind
<% end # object.kind? -%>
  result
end
<% if object.kind? -%>
# rubocop:enable Metrics/CyclomaticComplexity

def return_if_object(response, kind)
  self.class.return_if_object(response, kind)
end
<% else # object.kind? -%>

def return_if_object(response)
  self.class.return_if_object(response)
end
<% end # object.kind? -%>
